<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\admin\middleware\Auth;
use app\BaseController;
use app\common\model\User\Record as RecordModel;
use think\facade\Db;
use think\facade\View;

class Record extends BaseController
{

    protected $middleware = [
        Auth::class
    ];

    public function index()
    {
        $nav_title = [
            'nav_title' => '用户管理',
            'title' => '用户消费记录'
        ];

        // 接收查询字段
        $SearchField = $this->request->param('search_field','nickname');

        $keyword = $this->request->param('keyword','');

        $userid = $this->request->param('userid',0);

        $where = [];

        if(!empty($SearchField) && !empty($keyword))
        {
            $where[] = [$SearchField,'like',"%$keyword%"];
        }else{
            $keyword = '请输入相应的内容';
        }

        if(!empty($userid))
        {
            $where[] = ['userid', '=', $userid];
        }

        $RecordList = RecordModel::hasWhere('user',[['deletetime','EXP',Db::raw('IS NULL')]])->where($where)->order('createtime','desc')->paginate(['list_rows' => 5,'query' => request()->param()]);
    
        // 编号 currentPage 当前页 listRows 每页要显示多少条数据
        $num = ($RecordList->currentPage() - 1) * $RecordList->listRows();

        return View::fetch('',[
            'nav_title' => $nav_title,
            'RecordList' => $RecordList,
            'num' => $num,
            'SearchField' => $SearchField,
            'keyword' => $keyword
        ]);
    }

    // 删除
    public function del()
    {
        if($this->request->isAjax())
        {
            $recid = $this->request->param('recid',0);

            if(empty($recid))
            {
                $this->error('请选择需要删除的记录');
            }

            if(is_array($recid))
            {
                $RecordList = RecordModel::whereIn('id',$recid)->column('id');

                $NotId = '';

                foreach($recid as $item)
                {
                    if(!in_array($item,$RecordList))
                    {
                        $NotId .= $item . ',';
                    }
                }

                $NotId = rtrim($NotId,',');

                if(!empty($NotId))
                {
                    $this->error("ID：{$NotId}不存在，请重新选择");
                }

                $result = RecordModel::destroy($recid);

                if($result !== true)
                {
                    $this->error('批量删除失败');
                }else{
                    $this->success('批量删除成功');
                }
                
            }else{
                // 单条删除
                $record = RecordModel::find($recid);

                if(!$record)
                {
                    $this->error('该记录不存在，无法删除');
                }

                // 真实删除
                $result = RecordModel::destroy($recid);

                if($result !== true)
                {
                    $this->error('删除失败');
                }else{
                    $this->success('删除成功');
                }
            }
        }
    }
}
